﻿using FutianCloudAPIService.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FutianCloudAPIService.Domains.Winstone
{
    /// <summary>
    /// 祥云API
    /// </summary>
    public class WinStoneApi
    {
        private static string WinstoneUrl;
        private static string OrcKey ;
        private static string OrcSecret ;
        private const int CardTypeId = 20;

        static WinStoneApi()
        {
            WinstoneUrl = SystemDomain.WinStoneUrl;
            OrcKey = SystemDomain.OrcKey; 
            OrcSecret = SystemDomain.OrcSecret ;
        }

        #region Business card

        /// <summary>
        /// 识别名片
        /// </summary>
        /// <param name="imagePath">本地路径</param>
        /// <returns>返回识别的json</returns>
        public  async Task<string> Recognize(string imagePath)
        {
            using (var file = File.OpenRead(imagePath))
            {
                return await Recognize(file);
            }

        }

        /// <summary>
        /// 发送请求
        /// </summary>
        public  async Task<string> Recognize(Stream stream)
        {
            var task = WebUtil.Post(WinstoneUrl
               , new[] { new KeyValuePair<string,object>("file",stream)
                        , new KeyValuePair<string,object>("key",OrcKey)
                        , new KeyValuePair<string,object>("secret",OrcSecret)
                        , new KeyValuePair<string,object>("typeId",CardTypeId.ToString())
                        , new KeyValuePair<string,object>("format","json")
               }
           );

            var ret = await task;
            return ret;
        }


        #endregion
    }
}
